Change in openvpn[master]: Fix unaligned access in macOS/Solaris hwaddr
A message that is not in the archive (sent off-list or not yet received): gerrit.1700582804000.Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8@...2715...
Attention is currently required from: flichtenheld.
Hello flichtenheld,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
to review the following change.
Change subject: Fix unaligned access in macOS/Solaris hwaddr
......................................................................
Fix unaligned access in macOS/Solaris hwaddr
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
---
M src/openvpn/route.c
1 file changed, 33 insertions(+), 8 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/1
src/openvpn/route.c +33 -8
@@ -3643,7 +3643,6 @@if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3667,48 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires */ +memcpy (&ifr, cp,sizeof (struct ifreq ));#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); +const size_t len =sizeof (ifr.ifr_name) +sizeof (ifr.ifr_addr);#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len);#endif -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes in size and has +* 12 bytes space for the hw address (6 bytes) +* and Ethernet interface name (max 16 bytes) +* +* So if the interface name is more than 6 byte, it +* extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* Since we only copied 32 bytes from cp to ifr but sdl +* might extend after ifr's end, we need to copy from +* cp directly to avoid missing out on extra bytes +* behind the struct +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange
Attention is currently required from: flichtenheld, plaisthos.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS/Solaris hwaddr
......................................................................
Patch Set 1: Code-Review-2
(1 comment)
Patchset:
PS1:
this breaks OpenSolaris, as it has no sa_len element. I'll look into a good fix, but things have kept me busy.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Tue, 21 Nov 2023 18:56:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
[Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: flichtenheld, plaisthos.
Hello cron2, flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
to look at the new patch set (#2).
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for.
---
M src/openvpn/route.c
1 file changed, 38 insertions(+), 11 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/2
src/openvpn/route.c +38 -11
@@ -3639,11 +3639,15 @@ rgi->flags |=RGI_NETMASK_DEFINED ; } +#if !defined(TARGET_SOLARIS ) +/* Illumos/Solaris does not provide AF_LINK entries when calling the +* SIOCGIFCONF API, so there is little sense to trying to figure out a +* MAC address from an API that does not provide that information */ +/* try to read MAC addr associated with interface that owns default gateway */ if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3672,44 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; -#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); -#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); -#endif +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires */ +memcpy (&ifr, cp,sizeof (struct ifreq )); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len); -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes in size and has +* 12 bytes space for the hw address (6 bytes) +* and Ethernet interface name (max 16 bytes) +* +* So if the interface name is more than 6 byte, it +* extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* Since we only copied 32 bytes from cp to ifr but sdl +* might extend after ifr's end, we need to copy from +* cp directly to avoid missing out on extra bytes +* behind the struct +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
@@ -3691,6 +3717,7 @@ cp += len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done:if (sockfd >=0 )
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset
[Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Patch Set 2: Code-Review+2
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Wed, 22 Nov 2023 16:42:53 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
[Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: plaisthos.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Patch Set 2: Code-Review-1
(5 comments)
Patchset:
PS2:
The ifr memcpy() is well understood and necessary. The sockaddr_dl memcpy() needs at least some better comments, but maybe it's not necessary at all.
File src/openvpn/route.c:
http://gerrit.openvpn.net/c/openvpn/+/454/comment/b7cb53d2_d5e9c975 :
PS2, Line 3677: * requires */
"need to memcpy() to a strict ifr to force 8-byte alignment required for member access"
http://gerrit.openvpn.net/c/openvpn/+/454/comment/3ec0b40a_9dd8ee38 :
PS2, Line 3691: * struct sockaddr_dl is 20 bytes in size and has
On FreeBSD 13 this is
```
struct sockaddr_dl {
u_char sdl_len; /* Total length of sockaddr */
u_char sdl_family; /* AF_LINK */
u_short sdl_index; /* if != 0, system given index for interface */
u_char sdl_type; /* interface type */
u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
u_char sdl_alen; /* link level address length */
u_char sdl_slen; /* link layer selector length */
char sdl_data[46]; /* minimum work area, can be larger;
contains both if name and ll address */
};
```so the "20 bytes" comment should be qualified "on some of the BSDs" or so (NetBSD indeed has only 20 bytes). OpenBSD has
```
char sdl_data[24]; /* minimum work area, can be larger;
contains both if name and ll address;
big enough for IFNAMSIZ plus 8byte ll addr */
```http://gerrit.openvpn.net/c/openvpn/+/454/comment/5b60619c_c908b9eb :
PS2, Line 3693: * and Ethernet interface name (max 16 bytes)
the wording here confused me ("if it's hw address and interface name, in this order, why would we care about size of interface name?") - I'd swap the order around ("..for interface name (max 16 bytes) and hw address (max 6 bytes").
http://gerrit.openvpn.net/c/openvpn/+/454/comment/e6546e64_f9ad06e2 :
PS2, Line 3703: * Since we only copied 32 bytes from cp to ifr but sdl
If ifr_addr.sa_len is >16 (because "long address") the max() above will make us copy more than 32 bytes. So maybe we already have everything we need here, and can spare ourselves the double twist?
OTOH, the code below does max(sizeof(), sa_len) - so if sa_len is shorter, we might copy too much with the second copy?
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Sat, 30 Dec 2023 15:03:43 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
[Openvpn-devel] [S] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: plaisthos.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Patch Set 2:
(1 comment)
File src/openvpn/route.c:
http://gerrit.openvpn.net/c/openvpn/+/454/comment/ffdd8937_e4a1e87d :
PS2, Line 3703: * Since we only copied 32 bytes from cp to ifr but sdl
Quoted 1 line
If
ifr_addr. […]
Ah, no, it's not that easy. The code above does some arithmetics with len=...+max(), but then doesn't actually memcpy(..., len) bytes but sizeof(ifr).
Need to think through this again. But however, the comments need work to be understandable for mere mortals :-)
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Sat, 30 Dec 2023 15:46:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: cron2 <[email protected]...>
Gerrit-MessageType: comment
[Openvpn-devel] [M] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: cron2, flichtenheld, plaisthos.
Hello cron2, flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
to look at the new patch set (#3).
The following approvals got outdated and were removed:
Code-Review+2 by flichtenheld, Code-Review-1 by cron2
The change is no longer submittable: Code-Review and checks~ChecksSubmitRule are unsatisfied now.
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for.
Add includes stddef.h for offsetof and integer.h for max_int.
---
M src/openvpn/route.c
1 file changed, 46 insertions(+), 11 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/3
src/openvpn/route.c +46 -11
@@ -24,6 +24,7 @@/* * Support routines for adding/deleting network routes. */ +#include <stddef.h> #ifdef HAVE_CONFIG_H #include "config.h"
@@ -40,6 +41,7 @@#include "win32.h" #include "options.h" #include "networking.h" +#include "integer.h" #include "memdbg.h"
@@ -3639,11 +3641,15 @@ rgi->flags |=RGI_NETMASK_DEFINED ; } +#if !defined(TARGET_SOLARIS ) +/* Illumos/Solaris does not provide AF_LINK entries when calling the +* SIOCGIFCONF API, so there is little sense to trying to figure out a +* MAC address from an API that does not provide that information */ +/* try to read MAC addr associated with interface that owns default gateway */ if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3674,50 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; -#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); -#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); -#endif +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires. Need to memcpy() to a strict ifr to force 8-byte +* alignment required for member access */ +memcpy (&ifr, cp,sizeof (struct ifreq )); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len); -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes (on macOS and NetBSD, +* larger on other BSDs) in size and has +* 12 bytes space for the Ethernet interface name +* (max 16 bytes) and hw address (6 bytes) +* +* So if the interface name is more than 6 byte, the +* location of hwaddr extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* We only copied 32 bytes (size of ifr at least on macOS +* might differ on other platforms again) from cp to ifr. +* +* But as hwaddr might extend but sdl might extend beyond +* ifr's. So we need recalculate how large the actual size +* of the embedded dl_sock actually is and then also need +* to copy it since it also most likely does not have the +* proper alignment required to access the struct. +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
@@ -3691,6 +3725,7 @@ cp += len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done:if (sockfd >=0 )
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset
[Openvpn-devel] [M] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveAttention is currently required from: flichtenheld, plaisthos.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Patch Set 3: Code-Review+2
(1 comment)
Patchset:
PS3:
I still find this all horribly ugly, but this is not your fault... this API is bad enough (... on MacOS, it seems) but combined with C "undefined behaviour" rules there is no hope for sanity...
That said, I have tested this on FreeBSD (works), NetBSD (works), OpenBSD (no AF_LINK provided, so, didn't work before, doesn't break anything now), MacOS (works) and OpenSolaris (no AF_LINK provided, but it compiles and tests fine).
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Sun, 31 Dec 2023 17:33:53 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
PATCH v3 1/1 Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archiveFrom: Arne Schwabe <[email protected]...>
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for.
Add includes stddef.h for offsetof and integer.h for max_int.
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/454
This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>
src/openvpn/route.c +46 -11
@@ -24,6 +24,7 @@/* * Support routines for adding/deleting network routes. */ +#include <stddef.h> #ifdef HAVE_CONFIG_H #include "config.h"
@@ -40,6 +41,7 @@#include "win32.h" #include "options.h" #include "networking.h" +#include "integer.h" #include "memdbg.h"
@@ -3639,11 +3641,15 @@ rgi->flags |=RGI_NETMASK_DEFINED ; } +#if !defined(TARGET_SOLARIS ) +/* Illumos/Solaris does not provide AF_LINK entries when calling the +* SIOCGIFCONF API, so there is little sense to trying to figure out a +* MAC address from an API that does not provide that information */ +/* try to read MAC addr associated with interface that owns default gateway */ if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3674,50 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; -#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); -#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); -#endif +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires. Need to memcpy() to a strict ifr to force 8-byte +* alignment required for member access */ +memcpy (&ifr, cp,sizeof (struct ifreq )); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len); -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes (on macOS and NetBSD, +* larger on other BSDs) in size and has +* 12 bytes space for the Ethernet interface name +* (max 16 bytes) and hw address (6 bytes) +* +* So if the interface name is more than 6 byte, the +* location of hwaddr extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* We only copied 32 bytes (size of ifr at least on macOS +* might differ on other platforms again) from cp to ifr. +* +* But as hwaddr might extend but sdl might extend beyond +* ifr's. So we need recalculate how large the actual size +* of the embedded dl_sock actually is and then also need +* to copy it since it also most likely does not have the +* proper alignment required to access the struct. +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
@@ -3691,6 +3725,7 @@ cp += len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done:if (sockfd >=0 )
[Openvpn-devel] [PATCH applied] Re: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
There's still room for improvement in the comments, but the current version might be as good as it gets this year... and I want to be able to push to GHA again without UBSAN errors :-)
Tested this on NetBSD, OpenBSD, FreeBSD, MacOS and OpenSolaris, and found lots of interesting things - but nothing that was made worse by *this* patch, and the UBSAN build stopped complaining on MacOS... also, I've stared long and hard at the code, and as long as we can trust the kernel, every variant of short/long length should be covered.
Did I say that this is a shitty API that MacOS copied from FreeBSD, FreeBSD actually fixed it (by increasing the structure size a lot, so if the buffer is aligned, nothing will overrun and alignment will also be fine), and MacOS just stuck with what they have...
A followup patch that implements HWADDR for OpenSolaris happened by accident while testing this :-) - will be on the list soon.
Your patch has been applied to the master and release/2.6 branch (bugfix).
commit f13331005d5a75f2788685485d46be1fe2f133a1 (master) commit 5380fe02b9ef4f0f2b1f0eb52100b7922965dfdb (release/2.6)
Author: Arne Schwabe Date: Sun Dec 31 18:34:31 2023 +0100
Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27885.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [M] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archivecron2 has uploaded a new patch set (#4) to the change originally created by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
The following approvals got outdated and were removed:
Code-Review+2 by cron2
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for.
Add includes stddef.h for offsetof and integer.h for max_int.
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27885.html
---
M src/openvpn/route.c
1 file changed, 46 insertions(+), 11 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/454/4
src/openvpn/route.c +46 -11
@@ -24,6 +24,7 @@/* * Support routines for adding/deleting network routes. */ +#include <stddef.h> #ifdef HAVE_CONFIG_H #include "config.h"
@@ -40,6 +41,7 @@#include "win32.h" #include "options.h" #include "networking.h" +#include "integer.h" #include "memdbg.h"
@@ -3639,11 +3641,15 @@ rgi->flags |=RGI_NETMASK_DEFINED ; } +#if !defined(TARGET_SOLARIS ) +/* Illumos/Solaris does not provide AF_LINK entries when calling the +* SIOCGIFCONF API, so there is little sense to trying to figure out a +* MAC address from an API that does not provide that information */ +/* try to read MAC addr associated with interface that owns default gateway */ if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3674,50 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; -#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); -#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); -#endif +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires. Need to memcpy() to a strict ifr to force 8-byte +* alignment required for member access */ +memcpy (&ifr, cp,sizeof (struct ifreq )); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len); -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes (on macOS and NetBSD, +* larger on other BSDs) in size and has +* 12 bytes space for the Ethernet interface name +* (max 16 bytes) and hw address (6 bytes) +* +* So if the interface name is more than 6 byte, the +* location of hwaddr extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* We only copied 32 bytes (size of ifr at least on macOS +* might differ on other platforms again) from cp to ifr. +* +* But as hwaddr might extend but sdl might extend beyond +* ifr's. So we need recalculate how large the actual size +* of the embedded dl_sock actually is and then also need +* to copy it since it also most likely does not have the +* proper alignment required to access the struct. +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
@@ -3691,6 +3725,7 @@ cp += len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done:if (sockfd >=0 )
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset
[Openvpn-devel] [M] Change in openvpn[master]: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
In reply to a message not in the archivecron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/454?usp=email )
Change subject: Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
......................................................................
Fix unaligned access in macOS, FreeBSD, Solaris hwaddr
The undefined behaviour USAN clang checker found this.
This fix is a bit messy but so are the original structures.
Since the API on Solaris/Illuminos does not return the AF_LINK sockaddr type we are interested in, there is little value in fixing the code on that platform to iterate through a list that does not contain the element we are looking for.
Add includes stddef.h for offsetof and integer.h for max_int.
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27885.html
---
M src/openvpn/route.c
1 file changed, 46 insertions(+), 11 deletions(-)
src/openvpn/route.c +46 -11
@@ -24,6 +24,7 @@/* * Support routines for adding/deleting network routes. */ +#include <stddef.h> #ifdef HAVE_CONFIG_H #include "config.h"
@@ -40,6 +41,7 @@#include "win32.h" #include "options.h" #include "networking.h" +#include "integer.h" #include "memdbg.h"
@@ -3639,11 +3641,15 @@ rgi->flags |=RGI_NETMASK_DEFINED ; } +#if !defined(TARGET_SOLARIS ) +/* Illumos/Solaris does not provide AF_LINK entries when calling the +* SIOCGIFCONF API, so there is little sense to trying to figure out a +* MAC address from an API that does not provide that information */ +/* try to read MAC addr associated with interface that owns default gateway */ if (rgi->flags &RGI_IFACE_DEFINED ) {struct ifconf ifc; -struct ifreq *ifr;const int bufsize =4096 ;char *buffer;
@@ -3668,22 +3674,50 @@for (cp = buffer; cp <= buffer + ifc.ifc_len -sizeof (struct ifreq ); ) { - ifr = (struct ifreq *)cp; -#if defined(TARGET_SOLARIS ) -const size_t len =sizeof (ifr->ifr_name) +sizeof (ifr->ifr_addr); -#else -const size_t len =sizeof (ifr->ifr_name) +max (sizeof (ifr->ifr_addr), ifr->ifr_addr.sa_len); -#endif +struct ifreq ifr = {0 }; +/* this is not always using an 8 byte alignment that struct ifr +* requires. Need to memcpy() to a strict ifr to force 8-byte +* alignment required for member access */ +memcpy (&ifr, cp,sizeof (struct ifreq )); +const size_t len =sizeof (ifr.ifr_name) +max (sizeof (ifr.ifr_addr), ifr.ifr_addr.sa_len); -if (!ifr->ifr_addr.sa_family) +if (!ifr.ifr_addr.sa_family) {break ; } -if (!strncmp (ifr->ifr_name, rgi->iface,IFNAMSIZ )) +if (!strncmp (ifr.ifr_name, rgi->iface,IFNAMSIZ )) { -if (ifr->ifr_addr.sa_family ==AF_LINK ) +if (ifr.ifr_addr.sa_family ==AF_LINK ) { -struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; +/* This is a confusing member access on multiple levels. +* +* struct sockaddr_dl is 20 bytes (on macOS and NetBSD, +* larger on other BSDs) in size and has +* 12 bytes space for the Ethernet interface name +* (max 16 bytes) and hw address (6 bytes) +* +* So if the interface name is more than 6 byte, the +* location of hwaddr extends beyond the struct. +* +* This struct is embedded into ifreq that has +* 16 bytes for a sockaddr and also expects this +* struct to potentially extend beyond the bounds of +* the struct. +* +* We only copied 32 bytes (size of ifr at least on macOS +* might differ on other platforms again) from cp to ifr. +* +* But as hwaddr might extend but sdl might extend beyond +* ifr's. So we need recalculate how large the actual size +* of the embedded dl_sock actually is and then also need +* to copy it since it also most likely does not have the +* proper alignment required to access the struct. +*/ +const size_t sock_dl_len =max_int ((int ) (sizeof (struct sockaddr_dl )), + (int ) (ifr.ifr_addr.sa_len)); + +struct sockaddr_dl *sdl =gc_malloc (sock_dl_len, true, &gc); +memcpy (sdl, cp + offsetof(struct ifreq , ifr_addr), sock_dl_len);memcpy (rgi->hwaddr,LLADDR (sdl),6 ); rgi->flags |=RGI_HWADDR_DEFINED ; }
@@ -3691,6 +3725,7 @@ cp += len; } } +#endif /* if !defined(TARGET_SOLARIS) */ done:if (sockfd >=0 )
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/454?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia797c8801fa9a9bc10b6674efde5fdbd7132e4a8
Gerrit-Change-Number: 454
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged